<?php

namespace app\common\command;

use app\common\model\haikang\HkConsumerMsg;
use app\common\service\haikang\HaikangService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Queue;

/**
 * 海康获取消息相关命令行
 */
class HkConsumerMessages extends Command
{
    protected function configure()
    {
        $this->setName('hk_consumer_messages')
            ->setDescription('hk_consumer_messages');
    }

    protected function execute(Input $input, Output $output)
    {
        $get_messages = HaikangService::getMessages();
        $res = $get_messages['messages'];
        $consumerId = $get_messages['consumerId'];
        if ($res['code'] == 200 && !empty($res['data'])) {
            $data = $res['data'];
            foreach ($data as $val) {
                $val['consumerId'] = $consumerId;
                //摄像头捕捉违规事件
                if ($val['msgType'] == 'ai_message_task_result' || $val['msgType'] == 'open_event_access') {
                    $HkConsumerMsg = HkConsumerMsg::where(['msg_id' => $val['msgId']])->cache(290)->field('id')->find();
                    if (!empty($HkConsumerMsg)) continue;
                    // 1.当前任务将由哪个类来负责处理。
                    $jobHandlerClassName = 'app\queue\haikang\ConsumerMessagesQueue';
                    // 2.当前任务归属的队列名称，如果为新队列，会自动创建
                    $jobQueueName = "ConsumerMessagesQueue"; //php think queue:listen --queue ConsumerMessagesQueue
                    Queue::push($jobHandlerClassName, $val, $jobQueueName);
                }
            }
        }
        $output->writeln("OK" . date('Y-m-d H:i:s'));
    }
}